package com.newtouch.bxzs.enterprise.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.newtouch.bxzs.common.base.model.PersonalCustomer;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 个人客户mapper层
 *
 * <p>
 * <b>History:</b>
 * <table border="1">
 * <tr>
 * <th>Date</th>
 * <th>Operator</th>
 * <th>Memo</th>
 * </tr>
 * <tr>
 * <td>2020/12/22 10:44</td>
 * <td>lingshouyi</td>
 * <td>Create</td>
 * </tr>
 * </table>
 *
 * @author lingshouyi
 * @version 1.0.0
 * @since 1.0.0
 */
@Mapper
public interface PersonalCustomerMapper extends BaseMapper<PersonalCustomer> {

    @Select({"<script>" +
            "SELECT \n" +
            "DISTINCT c.CUSTOMER_ID,c.CUSTOMER_NAME,c.CUSTOMER_SEX,c.CUSTOMER_PHONE,c.CUSTOMER_TELEPHONE,c.CUSTOMER_CERTIFICATE_TYPE,c.CUSTOMER_CERTIFICATE_NUMBER,c.CUSTOMER_NAME_FULL_SPELLING,c.CUSTOMER_BIRTHDAY,c.CUSTOMER_OCCUPATION,c.CUSTOMER_FAMILY_ADDRESS,c.IS_SOCIAL_INSURANCE AS socialInsurance,c.CUSTOMER_COMPANY_NAME,c.CUSTOMER_COMPANY_ADDRESS,c.CUSTOMER_POSITION,c.CUSTOMER_ANNUAL_INCOME,c.CUSTOMER_MARRY_STATUS,c.CUSTOMER_CHILD_STATUS,c.CUSTOMER_HOBBY,c.CUSTOMER_LABEL,c.USER_JOB_NUMBER,c.MEMO,c.IS_DELETE \n" +
            "FROM \n" +
            "ASSISTANT_CUSTOMER c \n" +
            "LEFT JOIN \n" +
            "ASSISTANT_CUSTOMER_GROUP_MAP m \n" +
            "ON \n" +
            "m.CUSTOMER_ID = c.CUSTOMER_ID \n" +
            "LEFT JOIN \n" +
            "ASSISTANT_CUSTOMER_GROUP g \n" +
            "ON \n" +
            "g.GROUP_ID = m.GROUP_ID \n" +
            "WHERE 1=1 \n" +
            "<when test='customerName != null'> \n" +
            "AND (c.CUSTOMER_NAME like CONCAT('%',#{customerName},'%')) \n" +
            "</when> \n" +
            "<when test='groupId != null'> \n" +
            "AND m.GROUP_ID = #{groupId} \n" +
            "</when> \n" +
            "AND c.USER_JOB_NUMBER = #{userJobNumber} \n" +
            "AND c.IS_DELETE = 0 \n" +
            "ORDER BY c.CUSTOMER_NAME_FULL_SPELLING ASC \n" +
            "</script>"})
    List<PersonalCustomer> getCustomerList(@Param("customerName") String customerName, @Param("groupId") String groupId , @Param("userJobNumber") String userJobNumber);

    /**
     * 逻辑删除客户
     * @param customerId 客户id
     * @param deleteFlag 删除状态
     * @return 成功标识
     */
    @Update("update ASSISTANT_CUSTOMER set IS_DELETE = #{deleteFlag} where CUSTOMER_ID = #{customerId}")
    int deleteCustomer(@Param("customerId") String customerId,@Param("deleteFlag") Boolean deleteFlag);
}
